For this homework, we will use the msleep dataset provided by ggplot2. See here for details: https://ggplot2.tidyverse.org/reference/msleep.html

#head(msleep)
msleep %>%
  mutate(
    vore = case_when( #categorizing the ideal diet variable
      vore == "carni" ~ "Carnivore",
      vore == "omni" ~ "Omnivore",
      vore == "herbi" ~ "Herbivore",
      vore == "insecti" ~ "Insectivore",
      TRUE ~ NA_character_ # should never reach
    )
  )
## # A tibble: 83 × 11
##    name         genus vore  order conse…¹ sleep…² sleep…³ sleep…⁴ awake  brainwt
##    <chr>        <chr> <chr> <chr> <chr>     <dbl>   <dbl>   <dbl> <dbl>    <dbl>
##  1 Cheetah      Acin… Carn… Carn… lc         12.1    NA    NA      11.9 NA      
##  2 Owl monkey   Aotus Omni… Prim… <NA>       17       1.8  NA       7    0.0155 
##  3 Mountain be… Aplo… Herb… Rode… nt         14.4     2.4  NA       9.6 NA      
##  4 Greater sho… Blar… Omni… Sori… lc         14.9     2.3   0.133   9.1  0.00029
##  5 Cow          Bos   Herb… Arti… domest…     4       0.7   0.667  20    0.423  
##  6 Three-toed … Brad… Herb… Pilo… <NA>       14.4     2.2   0.767   9.6 NA      
##  7 Northern fu… Call… Carn… Carn… vu          8.7     1.4   0.383  15.3 NA      
##  8 Vesper mouse Calo… <NA>  Rode… <NA>        7      NA    NA      17   NA      
##  9 Dog          Canis Carn… Carn… domest…    10.1     2.9   0.333  13.9  0.07   
## 10 Roe deer     Capr… Herb… Arti… lc          3      NA    NA      21    0.0982 
## # … with 73 more rows, 1 more variable: bodywt <dbl>, and abbreviated variable
## #   names ¹​conservation, ²​sleep_total, ³​sleep_rem, ⁴​sleep_cycle

Problem 1: Visualize the relationship between total amount of sleep and body weight in each mammal classified as a carnivore (vore == "carni"). Your plot should include raw data points as well as a linear trend line with confidence interval. What do you observe?

#relationship between amount of sleep (sleep_total) and body weight (bodywt) in carnivores
msleep %>%
  mutate(
    vore = case_when( #categorizing the ideal diet variable
      vore == "carni" ~ "Carnivore",
      vore == "omni" ~ "Omnivore",
      vore == "herbi" ~ "Herbivore",
      vore == "insecti" ~ "Insectivore",
      TRUE ~ NA_character_ # should never reach
    ) 
    ) %>%
  filter(vore == "Carnivore") %>% #filter only carnivores
ggplot() +
  aes(sleep_total, bodywt) +
  geom_point() + 
  geom_smooth(method=lm, col = 'red') +
  labs( #adding labels
    title = "Total Sleep vs. Body Weight in Carnivores",
    x = "Total Sleep (hrs)",
    y = "Body Weight"
    ) +
  scale_fill_brewer(
    name = "",
    palette = "Set3" #custom palette
    ) +
  theme_bw( #adding a theme for visualization 
  ) + 
  theme( #aesthetics
    legend.position = "top",
    axis.line = element_line(colour = "black"), 
    panel.border = element_blank(),
    panel.background = element_blank()
    ) 

Looking at the data of Total Sleep vs. the Body Weight in carnivores generally shows a negatively sloping trend. This also seems to be the case when the outlier is removed. (Shown in plot below) Therefore, the overall trend for this data is that the greater the body weight of a carnivore, the more sleep the animal will require.

msleep %>%
  mutate(
    vore = case_when( #categorizing the ideal diet variable
      vore == "carni" ~ "Carnivore",
      vore == "omni" ~ "Omnivore",
      vore == "herbi" ~ "Herbivore",
      vore == "insecti" ~ "Insectivore",
      TRUE ~ NA_character_ # should never reach
    ) 
    ) %>%
  filter(vore == "Carnivore", bodywt < 800) %>% #filter only carnivores
ggplot() +
  aes(sleep_total, bodywt) +
  geom_point() + 
  geom_smooth(method=lm, col = 'red') +
  labs( #adding labels
    title = "Total Sleep vs. Body Weight in Carnivores (outlier removed)",
    x = "Total Sleep (hrs)",
    y = "Body Weight"
    ) +
  scale_fill_brewer(
    name = "",
    palette = "Set3" #custom palette
    ) +
  theme_bw( #adding a theme for visualization 
  ) + 
  theme( #aesthetics
    legend.position = "top",
    axis.line = element_line(colour = "black"), 
    panel.border = element_blank(),
    panel.background = element_blank()
    ) 

Problem 2: Write a function to create the plot above. Your function should have two inputs: data, which is the dataset to plot, and vore, which is a string indicating the vore type, such as "carni". Reproduce the plot using your new function.

make_vore_plot <- function(data, vore) {
  ggplot() +
    aes(data$sleep_total, data$bodywt) +
    geom_point() + 
    geom_smooth(method=lm, col = 'red') +
    ggtitle(glue("{vore}")) +
    labs( #adding labels
      x = "Total Sleep (hrs)",
      y = "Body Weight"
      ) +
    scale_fill_brewer(
      name = "",
      palette = "Set3" #custom palette
      ) +
    theme_bw( #adding a theme for visualization  
      ) + 
    theme( #aesthetics
      legend.position = "top",
      axis.line = element_line(colour = "black"), 
      panel.border = element_blank(),
      panel.background = element_blank()
      ) 
}

data_carni <- msleep %>%
  mutate(
    vore = case_when( #categorizing the ideal diet variable
      vore == "carni" ~ "Carnivore",
      vore == "omni" ~ "Omnivore",
      vore == "herbi" ~ "Herbivore",
      vore == "insecti" ~ "Insectivore",
      TRUE ~ NA_character_ # should never reach
    ) 
    ) %>%
  filter(vore == "Carnivore") 

make_vore_plot(data_carni, vore = "Carnivore")

Problem 3: Write code that automatically applies the function you created in Problem 2 to all vore types (you can exclude NA values). Do not write a for loop. How does the relationship between body weight and total amount of sleep vary across vores?

msleep %>%
  mutate(
    vore = case_when( #categorizing the ideal diet variable
      vore == "carni" ~ "Carnivore",
      vore == "omni" ~ "Omnivore",
      vore == "herbi" ~ "Herbivore",
      vore == "insecti" ~ "Insectivore",
      TRUE ~ NA_character_ # should never reach
    ) 
    ) %>%
  nest(data = -vore) %>%
  mutate(plots = map2(data, vore, make_vore_plot)) %>%
  pull(plots) %>%
  walk(print)

Generally, the trend is the same throughout the dataset– with the exception of Insectivores. Carnivores, Omnivores, Herbivores, and those that categorize as NA, have a negative trend line whereas the Insectivores have a positive trend line. Based on the dataset msleep, it is observed that mammals that weigh more require more sleep. This is the case in all “vores” except Insectivores.